package net.rrkk.photoman.controller;

import net.rrkk.photoman.entity.Photo;
import net.rrkk.photoman.service.PhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/photos")
public class PhotoController {
    
    @Autowired
    private PhotoService photoService;
    
    // 上传图片
    @PostMapping("/upload")
    public ResponseEntity<Photo> uploadPhoto(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "isPublic", defaultValue = "true") Boolean isPublic) {
        
        try {
            Photo photo = photoService.createPhoto(file, categoryId, title, description, isPublic);
            return new ResponseEntity<>(photo, HttpStatus.CREATED);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    
    // 获取所有图片
    @GetMapping
    public ResponseEntity<List<Photo>> getAllPhotos() {
        List<Photo> photos = photoService.getAllPhotos();
        return new ResponseEntity<>(photos, HttpStatus.OK);
    }
    
    // 根据ID获取图片
    @GetMapping("/{id}")
    public ResponseEntity<Photo> getPhotoById(@PathVariable Long id) {
        Optional<Photo> photo = photoService.getPhotoById(id);
        if (photo.isPresent()) {
            return new ResponseEntity<>(photo.get(), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
    
    // 根据分类ID查找图片
    @GetMapping("/category/{categoryId}")
    public ResponseEntity<List<Photo>> getPhotosByCategoryId(@PathVariable Long categoryId) {
        List<Photo> photos = photoService.getPhotosByCategoryId(categoryId);
        return new ResponseEntity<>(photos, HttpStatus.OK);
    }
    
    // 根据标题搜索图片
    @GetMapping("/search")
    public ResponseEntity<List<Photo>> searchPhotosByTitle(@RequestParam String title) {
        List<Photo> photos = photoService.searchPhotosByTitle(title);
        return new ResponseEntity<>(photos, HttpStatus.OK);
    }
    
    // 更新图片
    @PutMapping("/{id}")
    public ResponseEntity<Photo> updatePhoto(
            @PathVariable Long id,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "isPublic", required = false) Boolean isPublic) {
        
        Photo updatedPhoto = photoService.updatePhoto(id, categoryId, title, description, isPublic);
        if (updatedPhoto != null) {
            return new ResponseEntity<>(updatedPhoto, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
    
    // 删除图片
    @DeleteMapping("/{id}")
    public ResponseEntity<HttpStatus> deletePhoto(@PathVariable Long id) {
        photoService.deletePhoto(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}